package com.example.shoppingdemo.RKL.Persenter;

import com.example.shoppingdemo.BaseApplication;
import com.example.shoppingdemo.RKL.Bean.AmendBean;
import com.example.shoppingdemo.RKL.Bean.MsgBean;
import com.example.shoppingdemo.RKL.Bean.loginBean;
import com.example.shoppingdemo.RKL.Bean.siginBean;
import com.example.shoppingdemo.RKL.Model.AmendModel;
import com.example.shoppingdemo.RKL.Model.LoginModel;
import com.example.shoppingdemo.RKL.Model.MsgModel;
import com.example.shoppingdemo.RKL.Model.SiginModel;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class PersenterLoginAngSigin implements LoginAngSiginIPersenter{
    //短信验证
    List<MsgModel> msgModelList=new ArrayList<>();
    //注册
    List<SiginModel> siginModelList=new ArrayList<>();
    //登录
    List<LoginModel> loginModelList=new ArrayList<>();
    //修改密码
    List<AmendModel> amendModelList=new ArrayList<>();

    //短信验证码
    @Override
    public void persenterMsg(String phone,TransferMsg transferMsg) {
    BaseApplication.loginAndSigin.requestMsgList(phone)
    .map(new Func1<MsgBean, MsgModel>() {
        @Override
        public MsgModel call(MsgBean msgBean) {
            //实例化DxYzModel（接受返回参数的Model）
            MsgModel msgModel = new MsgModel();
            //将返回参数赋值给dxYzModel
            msgModel.setCode(msgBean.getData());
            //最后返回赋值后的Model
            return msgModel;
        }
    })
            //切换到子线程
            .subscribeOn(Schedulers.io())
            //切换回主线程
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<MsgModel>() {
                @Override
                public void onCompleted() {
                    //linsterer：传数据方法的名字
                    //将值放入方法中在最后的结果中返回
                    transferMsg.TransferMsg(msgModelList);
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(MsgModel msgModel) {
                    //将数据放入集合中
                    msgModelList.add(msgModel);
                }
            });
    }
//注册
    @Override
    public void persenterSigin(String phone, String cardNum, String membPwd,String name,TransferSigin transferSigin) {
        FormBody formBody=new FormBody.Builder()
                .add("phone",phone)
                .add("cardNum",cardNum)
                .add("membPwd",membPwd)
                .add("validCode",name)
                .build();
        BaseApplication.loginAndSigin.requestSiginList(formBody)
                .map(new Func1<siginBean, SiginModel>() {
                    @Override
                    public SiginModel call(siginBean siginbean) {
                        SiginModel siginModel = new SiginModel();
                        siginModel.setMmsg(siginbean.getData());
                        return siginModel;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SiginModel>() {
                    @Override
                    public void onCompleted() {
                        if ( transferSigin!= null) {
                            transferSigin.TransferSigin(siginModelList);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(SiginModel siginModel) {
                        siginModelList.add(siginModel);
                    }
                });
    }
    //登录
    @Override
    public void persenterLogin(String phone, String cardNum, String membPwd, String name, TransferLogin transferLogin) {
        FormBody formBody=new FormBody.Builder()
                .add("phone",phone)
                .add("cardNum",cardNum)
                .add("membPwd",membPwd)
                .add("validCode",name)
                .build();
        BaseApplication.loginAndSigin.requestLoginList(formBody)
                .map(new Func1<loginBean, LoginModel>() {
                    @Override
                    public LoginModel call(loginBean loginBean) {
                        LoginModel loginModel = new LoginModel();
                        loginModel.setMmsg(loginBean.getData());
                        return loginModel;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginModel>() {
                    @Override
                    public void onCompleted() {
                        if ( transferLogin!= null) {
                            transferLogin.TransferLogin(loginModelList);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(LoginModel loginModel) {
                        loginModelList.add(loginModel);
                    }
                });
    }
    //修改密码
    @Override
    public void persenterAmend(String phone, String cardNum, String membPwd, TransferAmend transferAmend) {
        FormBody formBody=new FormBody.Builder()
                .add("phone",phone)
                .add("cardNum",cardNum)
                .add("membPwd",membPwd)
                .build();
        BaseApplication.loginAndSigin.requestAmendList(formBody)
                .map(new Func1<AmendBean, AmendModel>() {
                    @Override
                    public AmendModel call(AmendBean amendBean) {
                        AmendModel amendModel = new AmendModel();
                        amendModel.setMmsg(amendBean.getData());
                        return amendModel;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AmendModel>() {
                    @Override
                    public void onCompleted() {
                        if ( transferAmend!= null) {
                            transferAmend.TransferAmend(amendModelList);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(AmendModel amendModel) {
                        amendModelList.add(amendModel);
                    }
                });
    }


    //短信验证码
    public interface TransferMsg{
        void TransferMsg(List<MsgModel> msgModelList);
    }
    //注册
    public interface TransferSigin{
        void TransferSigin(List<SiginModel> siginModelList);
    }
    //登录
    public interface TransferLogin{
        void TransferLogin(List<LoginModel> loginModelList);
    }
    //登录
    public interface TransferAmend{
        void TransferAmend( List<AmendModel> amendModelList);
    }
}
